define(function(require,exports,module){
	/**--tmpl-require--**/

	require('jupload');

	
	//加载依赖
	require("ajaxproxy");
	var common = require("common");

	//初始化AjaxProxy插件
	$('.ajaxproxy').AjaxProxy({
		dataType : "json",
		method : "post",
		formId : "content-add-form",
		formCheckHandler : function(form_id) {
			return common.formCheckHandler(form_id, true);
		},
		callbackDelay : __global.jdialog.timer,
		timeInterval : __global.jdialog.timer,
		callBack : function (data) {   /* 执行ajax之后的回调函数 */
			common.ajaxCallback(data);
		}
	});


	exports.init = function(){
		/**--tmpl-todo--**/


	};
	exports.ajaxResultByYu = function (url, data, callback, failback) {

		$.ajax({
			type: 'POST',
			url: url,
			data: data,
			async: false,
			cache: false,
			contentType: false,
			processData: false,
			dataType: 'json',

			success: function (thisData) {

				layer.closeAll("loading");

				if (thisData.code == '001') {
					if ($.isFunction(failback)) {
						failback(thisData);
					} else {
						layer.alert(thisData.message, {
							icon: 2, timeout: 1800, end: function () {

							}
						});

					}

				} else {
					if ($.isFunction(callback)) {
						callback(thisData);
					} else {
						layer.msg(thisData.message, {
							icon: 1, timeout: 1800, end: function () {
								location.reload(true);
							}
						});
					}
				}
			},
			error: function (xhr, ajaxOptions, thrownError) {
				layer.closeAll("loading");
				layer.alert("系统出了小差，请联系管理员！", {icon: 2});
			}
		});
	};
	loadupload('files-upload','drop-area','src',function(data){$('input[name="data[logo]"]').val(data.message);});

	function loadupload(filesUploadId,dropAreaId,formInputName,callbak) {

		var filesUpload = $('#'+filesUploadId);
		if (filesUpload) {

			var fileInput = filesUpload.css({height: 0, width: 0, 'overflow': 'hidden'});

			filesUpload.bind("change",function(){
				traverseFiles(this.files,filesUploadId,dropAreaId,formInputName,callbak);
			});

			$('#'+dropAreaId).click(function () {
				fileInput.click();
			}).show();
		}
	}


	function traverseFiles(files,filesUploadId,dropAreaId,formInputName,callbak) {
		if (typeof files !== "undefined") {

			file = files[0];
			uploadFile(files[0],filesUploadId,dropAreaId,formInputName,callbak);

		} else {
			fileList.innerHTML = "No support for the File API in this web browser";
		}
	}

	function uploadFile(file,filesUploadId,dropAreaId,formInputName,callback) {

		var img = document.createElement("img");
		var reader = new FileReader();

		if (typeof FileReader !== "undefined" && (/image/i).test(file.type)) {
			var size = document.getElementById(filesUploadId).files[0].size;


			reader.onload = (function (theImg) {
				var fd = new FormData();

				fd.append(formInputName,document.getElementById(filesUploadId).files[0]);

				exports.ajaxResultByYu(
					'/admin/upload/upload?fileInputName='+formInputName,
					fd,
					function (data) {
						callback(data);
						if(data.code=='000'){
                            $("#"+dropAreaId).html(img);
                        }
					});
				theImg.onload = function () {
					if (size > 1024 * 1024) {


					} else {

					}
					//$("#"+dropAreaId).html(img);

				}
				return function (evt) {
					theImg.src = evt.target.result;
				};
			}(img));

			reader.readAsDataURL(file);
		}
	}
});
